import store from "@store/index";
(function () {
    let orientation = window.matchMedia("(orientation: portrait)");
    let width = document.documentElement.getBoundingClientRect().width; //获取宽度
    function onMatchMeidaChange(orientation) {
      if (orientation.matches) {
        // 竖屏
        width = document.documentElement.getBoundingClientRect().width; //获取竖屏宽度
        setTimeout(() => {
          // 重新计算竖屏宽度rem
          autoRootFontSize()
        });
      } else {
        // 横屏
        width = document.documentElement.getBoundingClientRect().width; //获取横屏宽度
        setTimeout(() => {
          // 重新计算横屏宽度rem
          autoRootFontSize()
        });
      }
    }
    onMatchMeidaChange(orientation);
    orientation.addListener(onMatchMeidaChange);
    
    /* 计算rem */
    function autoRootFontSize() {
      let setSize = Math.max(document.documentElement.getBoundingClientRect().width,1000) / 1920 * 16;  
      document.documentElement.style.fontSize = (setSize > 16 ? 16 : setSize) + 'px';
      let screenWidth = document.documentElement.clientWidth || document.body.clientWidth;
      if (screenWidth < 768) store.commit('setIsMobile', true);
      else store.commit('setIsMobile', false);
    }
    window.addEventListener('resize', autoRootFontSize);
    autoRootFontSize();
  })();
